package com.kla.power.controller;


import com.kla.power.config.LogPointer;
import com.kla.power.entity.ScreenMerchantEntity;
import com.kla.power.entity.UserEntity;
import com.kla.power.service.IScreenMerchantService;
import com.kla.power.util.RedisUtils;
import com.kla.power.util.ResultBody;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WuZH
 * @since 2024-08-06
 */
@RestController
@RequestMapping ("/screenMerchant")
@CrossOrigin (value = "*", maxAge = 3600)
public class ScreenMerchantController {

    @Resource
    private IScreenMerchantService iScreenMerchantService;

    @RequestMapping ("queryList")
    @LogPointer ("查询商户下大屏列表")
    public ResultBody queryList ( @RequestBody ScreenMerchantEntity screenMerchantEntity) {
        UserEntity userEntity = (UserEntity) RedisUtils.get ( screenMerchantEntity.getToken () );
        screenMerchantEntity.setMerchantId ( userEntity.getMerchantId () );
        List<ScreenMerchantEntity> screenMerchantEntities = iScreenMerchantService.queryList ( screenMerchantEntity );
        return ResultBody.success ( screenMerchantEntities );
    }

    @RequestMapping ("saveSingle")
    @LogPointer ("保存商户下大屏")
    public ResultBody saveSingle (@RequestBody ScreenMerchantEntity screenMerchantEntity) {
        UserEntity userEntity = (UserEntity) RedisUtils.get ( screenMerchantEntity.getToken () );
        screenMerchantEntity.setMerchantId ( userEntity.getMerchantId () );
        screenMerchantEntity = iScreenMerchantService.saveSingle ( screenMerchantEntity );
        return ResultBody.success ( screenMerchantEntity );
    }

    @RequestMapping ("updateSingle")
    @LogPointer ("更新商户大屏")
    public ResultBody updateSingle (@RequestBody ScreenMerchantEntity screenMerchantEntity) {
        UserEntity userEntity = (UserEntity) RedisUtils.get ( screenMerchantEntity.getToken () );
        screenMerchantEntity.setMerchantId ( userEntity.getMerchantId () );
        screenMerchantEntity = iScreenMerchantService.updateSingle ( screenMerchantEntity );
        return ResultBody.success ( screenMerchantEntity );
    }

}
